package com.hypo.controller;

import com.hypo.pojo.Result;
import com.hypo.untils.AliyunOSSOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //获取文件后缀
        String exName = originalFilename.substring(originalFilename.lastIndexOf("."));
        //重新定义文件名
        String uniqueFileName = UUID.randomUUID().toString().replace("-", "") + exName;

        //上传文件
        String url = aliyunOSSOperator.upload(file.getBytes(), uniqueFileName);
        return Result.success(url);
    }
}
